package com.ytjj.qmyx.mall.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 *
 * @version 1.0
 *
 */
public class DateUtils {

    /**
     * @param oldTime 较小的时间
     * @param newTime 较大的时间 (如果为空   默认当前时间 ,表示和当前时间相比)
     * @return -1 ：同一天.    0：昨天 .   1 ：至少是前天.
     * .
     */
    public static int isYeaterday(Date oldTime, Date newTime) throws ParseException {
        if (newTime == null) {
            newTime = new Date();
        }
        //将下面的 理解成  yyyy-MM-dd 00：00：00 更好理解点
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String todayStr = format.format(newTime);
        Date today = format.parse(todayStr);
        //昨天 86400000=24*60*60*1000 一天
        if ((today.getTime() - oldTime.getTime()) > 0 && (today.getTime() - oldTime.getTime()) <= 86400000) {
            return 0;
        } else if ((today.getTime() - oldTime.getTime()) <= 0) { //至少是今天
            return -1;
        } else { //至少是前天
            return 1;
        }

    }

    /**
     * 获取当前时间到第二天凌晨的秒数
     *
     * @return
     */
    public static Long getCurTimeDiff() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

    /**
     * 获取当前时间到第30天凌晨的秒数
     *
     * @return
     */
    public static Long getCurTimeDiff30() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 30);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

    public static long addDate(Date date, int n){
        Calendar cd = Calendar.getInstance(); //获取一个Calendar对象
        cd.setTime(date); //设置calendar日}catch(ParseException ee.printStackTrace()}
        // cd.add(Calendar.DATE, n); //增加n天
        // cd.add(Calendar.YEAR, n); //增加n年
        cd.add(Calendar.MONTH, n); //增加n个月
        System.out.println(cd.getTimeInMillis()/1000);
        return cd.getTimeInMillis()/1000;
    }

    /**
     * 今天开始时间到当前时间
     * @return
     */
    public static Map<String, Object> curDay() {
        LocalDate ld = LocalDate.now(ZoneId.systemDefault());
        LocalDateTime localDateTime = ld.atStartOfDay();

        Date startTime = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

        LocalDateTime ldt_end = LocalDateTime.now(ZoneId.systemDefault());
        Date endTime = Date.from(ldt_end.atZone(ZoneId.systemDefault()).toInstant());

        Map<String, Object> result = new HashMap<>();
        result.put("dayStart", startTime);
        result.put("dayEnd", endTime);
        return result;
    }

    /**
     * 获取昨天开始时间和结束时间
     *
     * @return
     */
    public static Map getYesterdayTime() {
        Long startTime = getBeginDayOfYesterday();
        Long endTime = getEndDayOfYesterDay();
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTimeStr = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault()));
        String endTimeStr = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(endTime), ZoneId.systemDefault()));
        Map map = new HashMap();
        map.put("startDate", startTimeStr);
        map.put("endDate", endTimeStr);
        return map;
    }

    public static Long getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTimeInMillis();
    }

    public static Long getEndDayOfYesterDay() {

        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.add(Calendar.DAY_OF_MONTH, -1);


        return cal.getTimeInMillis();
    }

    /**
     * 得到上周开始截至时间
     * @return
     */
    public static Map<String, Object> upWeek() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ");//注意后面有一个空格
        Calendar calStart = Calendar.getInstance();
        Calendar calEnd = Calendar.getInstance();
        calStart.add(Calendar.DAY_OF_MONTH,-7);//上一周
        calEnd.add(Calendar.DAY_OF_MONTH,-7);
        calStart.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calEnd.setFirstDayOfWeek(Calendar.MONDAY);
        calEnd.set(Calendar.DAY_OF_WEEK, calEnd.getFirstDayOfWeek() + 6);
        Timestamp weekStart = Timestamp.valueOf(format.format(calStart.getTime()) + "00:00:00");//字符串类型转化为timestamp
        Timestamp weekEnd = Timestamp.valueOf(format.format(calEnd.getTime()) + "23:59:59");
        System.out.println(weekStart);
        System.out.println(weekEnd);

        Map<String, Object> result = new HashMap<>();
        result.put("weekStart", weekStart);
        result.put("weekEnd", weekEnd);
        return result;
    }

    /**
     * 本周开始时间
     * @return
     */
    public static Date getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    // 获得本周日24点时间
    public static Date getTimesWeeknight() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getTimesWeekmorning());
        cal.add(Calendar.DAY_OF_WEEK,6);
        return cal.getTime();
    }



    /**
    * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
    * @param nowTime 当前时间
    * @param startTime 开始时间
    * @param endTime 结束时间
    * @returns: boolean
    * 
    */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /** 
    * 判断当前时间是否在[startTime, endTime]区间，参数为字符串格式HH:mm
    * @param nowTimeStr:
    * @param startTimeStr:
    * @param endTimeStr:
    * @returns: boolean 
    * 
    */
    public static boolean isEffectiveTimeStr(String nowTimeStr, String startTimeStr, String endTimeStr,String format){
        boolean runFlag = false;
        try {
            Date nowTime = new SimpleDateFormat(format).parse(nowTimeStr);
            Date startTime = new SimpleDateFormat(format).parse(startTimeStr);
            Date endTime = new SimpleDateFormat(format).parse(endTimeStr);
            if (isEffectiveDate(nowTime, startTime, endTime)) {
                runFlag = true;
            } else {
                runFlag = false;
            }
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return runFlag;
    }


    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     * @param beforeDate 20210711
     * @param afterDate 20210727
     * @return 16
     */
    public static int differentDaysByMillisecond(Date beforeDate, Date afterDate)
    {
        int days = (int) ((afterDate.getTime() - beforeDate.getTime()) / (1000*3600*24));
        return days;
    }

    public static void main(String[] args) {
       /* Calendar calendar = Calendar.getInstance();
        int weeks = calendar.getWeeksInWeekYear();
        System.out.println(weeks);

        System.out.println(getTimesWeeknight());*/
        System.out.println(isEffectiveTimeStr("07:59:59", "8:00:01", "24:00:00", "HH:mm:ss"));


    }

}
